package gin.dev.notification;

import gin.dev.update.DownloadNotification;
import gin.dev.update.UpdateTask;
import android.content.Context;
import android.util.Log;

public class NotificationBroker {

	public static int NON_NOTIFICATION = -1;
	public static int INFORMATION_NOTIFICATION = 0;
	
	// const for update app
	public static int UPDATE_NOTIFICATION = 1;
	public static String UPDATE_NOTIFICATION_PATTERN = "<1>";
	public static int UPDATE_NUMBER_PARAM = 4;
	public static int UPDATE_ID_PARAM_PACKAGENAME = 3;
	public static int UPDATE_ID_PARAM_VERSION = 1;	
	public static int UPDATE_ID_PARAM_URL = 2;
	public static int UPDATE_ID_PARAM_APPNAME = 0;
	
	// const for hot app intro
	public static int HOT_APP_NOTIFICATION = 2;
	public static String HOT_APP_NOTIFICATION_PATTERN = "/";
	public static int HOT_APP_NUMBER_PARAM = 2;
	public static int HOT_APP_ID_PARAM_APPNAME = 1;
	public static int HOT_APP_ID_PARAM_APPID = 0;	
	
	public static int Broker(Context context, String message)
	{
		Log.i("TAG", "google sms: " + message);
		if(message.contains(UPDATE_NOTIFICATION_PATTERN) == true)
		{
			String[] strs = message.split(UPDATE_NOTIFICATION_PATTERN);
			if(strs.length < UPDATE_NUMBER_PARAM)
				return NON_NOTIFICATION;
			String packagename = strs[UPDATE_ID_PARAM_PACKAGENAME].trim();
			String version = strs[UPDATE_ID_PARAM_VERSION].trim();
			String url = strs[UPDATE_ID_PARAM_URL].trim();
			UpdateTask.Update(context, packagename, version, url);
			return UPDATE_NOTIFICATION;
		}
		
		if(message.contains(HOT_APP_NOTIFICATION_PATTERN) == true)
		{
			String[] strs = message.split(HOT_APP_NOTIFICATION_PATTERN);
			if(strs.length < HOT_APP_NUMBER_PARAM)
				return NON_NOTIFICATION;
			String appname = strs[HOT_APP_ID_PARAM_APPNAME].trim();
			int productID = Integer.parseInt(strs[HOT_APP_ID_PARAM_APPID].trim());
			DownloadNotification.HotAppNotify(appname, productID, context);
			return HOT_APP_NOTIFICATION;
		}
		return INFORMATION_NOTIFICATION;
	}
}
